const app = getApp()
import {
  meState,
  changeHome,
  distance
} from "../../config/https";
//我的状态、修改我的状态、接单距离
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maskShow: false,
    type: '',
    selectType: '',
    workState: ['空闲', '忙碌'],
    workType: ['全部', '包工', '点工'],
    masterType: ['全部', '工装', '家装'],
    distanceType: [],
    oldDistance: [],
    successRate: '',  //填单成功率
    myState: '', //工作状态
    contractWork: '', //包工状态
    myType: '', //做工装家装
    distance: '', //距离
    distanceBtn: true //是否显示距离
  },
  getData() {
    wx.showLoading({
      title: '加载中'
    })
    meState({
      token: wx.getStorageSync('token')
    }).then(res => {
      wx.hideLoading()
      if (res.code == 1) {
        this.setData({
          successRate: res.data.success_rate,
          myState: res.data.work_type ? res.data.work_type : '未选择',
          contractWork: res.data.spot_type ? res.data.spot_type : '未选择',
          myType: res.data.clothes_type ? res.data.clothes_type : '未选择',
          distance: res.data.distance ? res.data.distance : '未选择',
          distanceBtn: res.data.is_distance == 1 ? true : false
        })
      }
    })
  },
  selectDistance(){
    distance().then(res => {
      if(res.code == 1){
        let distance = res.data
        let newArr = distance.map(item => {
          return item.title
        })
        this.setData({
          distanceType: newArr,
          oldDistance: distance
        })
      }
    })
  },
  goRecord() {
    wx.navigateTo({
      url: '/pages/orderRecord/orderRecord',
    })
  },
  ShowMask(e) {
    let type = Number(e.currentTarget.dataset.type)
    console.log(type)
    let selectType = ''
    switch (type) {
      case 1:
        selectType = this.data.workState
        break;
      case 2:
        selectType = this.data.workType
        break;
      case 3:
        selectType = this.data.masterType
        break;
      case 4:
        selectType = this.data.distanceType
        break;
    }
    this.setData({
      selectType,
      type,
      maskShow: true
    })
  },
  closeMask() {
    this.setData({
      maskShow: false
    })
  },
  confirmClick(e) {
    let obj = {
      token: wx.getStorageSync('token')
    }
    let selectKey = ''
    let selectVal = ''
    let val = e.detail.value
    let type = Number(this.data.type)
    switch (type) {
      case 1:
        obj.work_type = val
        selectKey = 'myState'
        selectVal = val
        break;
      case 2:
        obj.spot_type = val
        selectKey = 'contractWork'
        selectVal = val
        break;
      case 3:
        obj.clothes_type = val
        selectKey = 'myType'
        selectVal = val
        break;
      case 4:
        obj.distance_id = this.data.oldDistance[e.detail.index].id
        selectKey = 'distance'
        selectVal = val
        break;
    }
    this.changeState(obj)
    console.log(selectKey,selectVal)
    this.setData({
      [selectKey]: [selectVal],
      maskShow: false
    })
  },
  goHomePage() {
    wx.navigateTo({
      url: '/pages/homepage/homepage',
    })
  },
  distanceChange(e) {
    let obj = {
      token: wx.getStorageSync('token')
    }
    obj.is_distance = e.detail ? 1 : 2
    this.changeState(obj)
    this.setData({
      distanceBtn: e.detail
    })
  },
  goIntroduce() {
    wx.navigateTo({
      url: '/pages/workIntroduce/workIntroduce',
    })
  },
  changeState(data){
    console.log(data)
    changeHome(data).then(res => {
      if(res.code == 1){
        app.$tip(res.msg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
    this.selectDistance()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})